//
//  QDSicknessDetailController.m
//  simulateQuickDoctor
//
//  Created by locklight on 17/3/26.
//  Copyright © 2017年 PipiXia. All rights reserved.
//

#import "QDSicknessDetailController.h"
#import "QDNetworkTool.h"
#import <MJRefresh.h>
#import "YYModel.h"
#import "QDSicknessModel.h"
#import "QDBannerTools.h"


static NSString *reusedIllnessDetailCell = @"reusedIllnessDetailCell";

@interface QDSicknessDetailController () <UITableViewDelegate,UITableViewDataSource>

/**
 *  tableView
 */
@property (nonatomic, strong) UITableView *tableView;


/**
 *  疾病模型数组
 */
@property (nonatomic, strong) NSMutableArray *illnessArray;

@property (nonatomic, assign) NSInteger lastId;

@end

@implementation QDSicknessDetailController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [QDBannerTools pp_BannerTitle:@"加载中" andTime:1];
    
    // 设置导航栏
    [self setNavgationBar];
    
    [self.view addSubview:self.tableView];
    
    // 设置上下拉刷新
    [self setupMJRefresh];
}

/**
 *  设置navgationBar
 */
- (void)setNavgationBar {
    UIButton *button = [[UIButton alloc] init];
    button.frame = CGRectMake(0, 0, 50, 40);
    [button setImage:[UIImage imageNamed:@"home_nav_button_back"] forState:UIControlStateNormal];
    button.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [button addTarget:self action:@selector(didBackButton:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftItem;
}

/**
 *  设置上下拉刷新
 */
- (void)setupMJRefresh {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getMoreIllnessModel)];
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [footer setTitle:@"数据加载完毕" forState:MJRefreshStateNoMoreData];
    self.tableView.mj_footer = footer;
    [self.tableView.mj_footer beginRefreshing];
}

/**
 *  上拉加载数据
 */
- (void)loadData {
    NSString *url = @"http://iosapi.itcast.cn/doctor/searchCI3List.json.php";
    NSDictionary *parameters = @{@"page":@1,@"page_size":@15,@"ci1_id":@(self.illnessIndex),@"keyword":@""};
    
    QDNetworkTool *tool = [QDNetworkTool sharedTool];
    
    tool.requestSerializer = [AFJSONRequestSerializer serializer];
    [tool requestWithHTTPMethod:@"POST" URLString:url parameters:parameters CallBack:^(id responseObj) {
        if(responseObj != nil){
            NSString* result = responseObj[@"msg"];
            
            if([result isEqualToString:@"OK"]){
                NSArray* dataArr = responseObj[@"data"];
                for (NSDictionary* dict in dataArr) {
                    QDSicknessModel *model = [QDSicknessModel yy_modelWithDictionary: dict];
                    if (model.ci3_id > self.lastId) {
                        [self.illnessArray addObject:model];
                        self.lastId = model.ci3_id;
                    }
                }
                [QDBannerTools pp_BannerSuccessTitle:@"加载中" andTime:1];
                
                //获取数据后刷新
                [self.tableView reloadData];
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [QDBannerTools pp_BannerErrorTitle:@"请求参数错误" andTime:1];
            }
        }else{
            [QDBannerTools pp_BannerErrorTitle:@"网络请求错误" andTime:1];
        }
    }];
}


/**
 *  下拉刷新获取数据
 */
- (void)getMoreIllnessModel {
    
    [self.tableView reloadData];
    [self.tableView.mj_header endRefreshing];
}

#pragma mark - UITableViewdelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.illnessArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reusedIllnessDetailCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reusedIllnessDetailCell"];
    }
    
    cell.textLabel.attributedText = nil;
    QDSicknessModel *model =  self.illnessArray[indexPath.row];
    cell.textLabel.text = model.ci3_name;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *chooseString;
    
    QDSicknessModel *model = self.illnessArray[indexPath.row];
    chooseString = model.ci3_name;
    
    if (self.getIllnessString) {
        self.getIllnessString(chooseString,indexPath.row);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

/**
 *  返回按钮触发事件
 */
- (void)didBackButton:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 懒加载

- (NSMutableArray *)illnessArray {
    
    if (!_illnessArray) {
        _illnessArray = [NSMutableArray array];
    }
    return _illnessArray;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    
    return _tableView;
}

@end
